package cn.edu.cug.cs.gtl.apis.ui.actions;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.devicetree.DeviceTree;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.tree.TreePath;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

public class DeviceTreeMouseListener extends MouseInputAdapter {
    private AbstractApplication theApp=null;

    public DeviceTreeMouseListener(AbstractApplication theApp) {
        super();
        this.theApp=theApp;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        super.mouseReleased(e);

        if(SwingUtilities.isRightMouseButton(e)){
            DeviceTree tree= theApp.getMainFrame().getDevicePanel().getTreeComponent();
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            tree.setSelectionPath(selPath);
            if (selRow>-1){
                tree.setSelectionRow(selRow);
            }
            else{
                tree.setSelectionRow(0);
            }
            if(DeviceTree.isConnectionNode(tree.getCurrentSelectedNode())){
                JPopupMenu popupMenu = DeviceTree.createPopupMenuForConnectionNode();
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
            else{
                JPopupMenu popupMenu = DeviceTree.createPopupMenuForNonConnectionNode();
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }

        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        super.mouseWheelMoved(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
    }

}
